package com.hotec.PROJECT_NAME.modules.sys.controller;

import com.hotec.PROJECT_NAME.component.basic.BaseController;
import com.hotec.PROJECT_NAME.component.basic.ConstantKey;
import com.hotec.PROJECT_NAME.component.basic.JsonContent;
import com.hotec.PROJECT_NAME.config.encrypt.ApiEncrypt;
import com.hotec.PROJECT_NAME.modules.sys.entity.SysAccount;
import com.hotec.PROJECT_NAME.modules.sys.service.SysAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author llb
 * @date
 */
@RestController
@RequestMapping("sys/account")
public class SysAccountController extends BaseController {
    @Autowired
    SysAccountService service;


    @GetMapping("info")
    public JsonContent info() {
        SysAccount info = service.info(this.getCurrentAccountId(true));
        return success(info);
    }

    /**
     * 根据主键查询对象
     *
     * @param id
     * @return
     */
    @GetMapping("get")
    public JsonContent get(String id) {
        return this.success("获取成功", service.get(id), true);
    }


    /**
     * 获取用户,同时获取用户角色
     *
     * @param id
     * @return
     */
    @GetMapping("getWithRole")
    public JsonContent getWithRole(String id) {
        return this.success("获取成功", service.getWithRole(id), true);
    }


    @PostMapping("rePassword")
    public JsonContent rePwd(String username, String pwd) {
        service.resetPwd(username, pwd);
        return this.success();
    }

    /**
     * 根据对象列表逻辑删除
     *
     * @param entityList
     * @return
     */
    @PostMapping("delete")
    public JsonContent delete(@RequestBody List<SysAccount> entityList) {
        service.deleteById(entityList);
        return this.success().setMessage("删除成功！");
    }

    /**
     * 保存更新并且授权角色
     *
     * @return
     */
    @PostMapping("saveAndAccredit")
    @ApiEncrypt(type = SysAccount.class, responseEncrypt = false)
    public JsonContent save(SysAccount entity) {
        service.saveAndAccreditAndBindCommunity(entity);
        return this.success().setMessage("操作成功！");
    }

    /**
     * 保存更新并且授权角色
     *
     * @return
     */
    @PostMapping("saveUser")
    public JsonContent saveUser(@Valid @RequestBody SysAccount entity) {
        return this.success(service.saveUser(entity));
    }

    /**
     * 根据对象条件，查询单条数据
     *
     * @return
     */
    @PostMapping("getOne")
    public JsonContent getOne(@RequestBody SysAccount entity) {
        return this.success(service.getOne(entity));
    }
    /**
     * 修改密码
     *
     * @param sysAccount
     * @return
     */
    @PostMapping("modifyPassword")
    public JsonContent modifyPassword(@RequestBody SysAccount sysAccount) {
        service.modifyPassword(sysAccount);

        return this.success(service.get(sysAccount.getId()));
    }

    /**
     * 微信登录忘记密码
     *
     * @param sysAccount
     * @return
     */
    @PostMapping("forgetPassword")
    public JsonContent forgetPassword(@RequestBody SysAccount sysAccount) {
        service.forgetPassword(sysAccount);
        return this.success().setData(new HashMap<>());
    }

    /**
     * 更新用户名，以及电话
     */
    @PostMapping("updateUser")
    @Transactional
    public JsonContent updateUser(@Valid @RequestBody SysAccount entity) {
        SysAccount sysAccount = service.get(entity.getId());
        sysAccount.setMobile(entity.getMobile());
        sysAccount.setNickname(entity.getNickname());
        this.service.update(sysAccount);
        return this.success();
    }


    /**
     * 查询全部
     */
    @PostMapping("queryAll")
    public JsonContent queryAll(@RequestBody LinkedHashMap entity) {
        return this.success(service.queryAll(entity));
    }

    @PostMapping("query")
    public JsonContent query(@RequestParam(required = false, value = "pageNum", defaultValue = ConstantKey.DEFAULT_PAGE_NUM) Integer pageNum,
                             @RequestParam(required = false, value = "pageSize", defaultValue = ConstantKey.DEFAULT_PAGE_SIZE) Integer pageSize,
                             @RequestBody LinkedHashMap param) {

        return this.success(service.queryByPage(param,pageNum,pageSize, "create_time desc"));
    }
}


